// pages/install-report/index.js
import { getMonthStr } from "../../utils/util";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    monthShow: false,
    hasMore: true,
    report_month: "",
    report_type: "",
    param: {
      page: 1,
      pageSize: 10,
    },
    types: [
      {
        label: "不限",
        value: "0",
      },
      {
        label: "装饰公司安装",
        value: "1",
      },
      {
        label: "水电工安装",
        value: "2",
      },
      {
        label: "设计师安装",
        value: "3",
      },
      {
        label: "项目经理安装",
        value: "4",
      },
    ],
  },

  handleTypeChange(event) {
    // console.log(event);
    const { value } = event.detail;
    const { param } = this.data;

    param.page = 1;

    this.setData({
      report_type: value,
      param,
      list: [],
    });

    this.getData();
  },

  getMore() {
    const { param, hasMore } = this.data;

    if (!hasMore) return;

    param.page++;

    this.setData({
      param,
    });

    this.getData();
  },

  handleMonthChange(event) {
    const { value } = event.detail;
    const { param } = this.data;

    param.page = 1;

    this.setData({
      report_month: value,
      param,
      list: [],
    });

    this.getData();
  },

  handleToDetail(event) {
    const { id } = event.currentTarget.dataset;

    wx.navigateTo({
      url: "/pages/submit-install-report/index?id=" + id,
    });
  },

  getData() {
    const { param, report_month, report_type } = this.data;

    const params =
      report_type && report_type !== "0"
        ? {
            report_month: report_month || getMonthStr(),
            report_type,
            ...param,
          }
        : {
            report_month: report_month || getMonthStr(),
            ...param,
          };

    wx.ajax.post("queryDataReportPage.action", params, true).then((res) => {
      console.log(res);
      if (res.success) {
        const result = res.result;
        const oriList = this.data.list;

        this.setData({
          list: oriList.concat(result.pageList || []),
          hasMore: result.totalPages > result.page,
        });
      }
    });
  },

  handleToAddPage() {
    wx.navigateTo({
      url: "/pages/submit-install-report/index",
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { param } = this.data;

    param.page = 1;

    this.setData({
      param,
      list: [],
    });

    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
